{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 文档的总结、精炼、翻译"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8b16a5440350ade7"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 加载文档\n",
    "with open(\"letter.txt\") as f:\n",
    "    content = f.read()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "acdc8d77e7227843"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-zk2f3fc211ff0ad224ecd79efe339d0861d66379d4d11320\"\n",
    "os.environ[\"OPENAI_PROXY\"] = \"https://flag.smarttrot.com/v1/\"\n",
    "\n",
    "\n",
    "load_dotenv(\"openai.env\")\n",
    "OPENAI_API_KEY = os.getenv(\"OPENAI_API_KEY\")\n",
    "OPENAI_PROXY = os.getenv(\"OPENAI_PROXY\")\n",
    "OPENAI_MODEL = \"gpt-3.5-turbo-16k\"\n",
    "OPENAI_TOKEN_LIMIT = 8000\n",
    "\n",
    "from doctran import Doctran\n",
    "doctrans = Doctran(\n",
    "    openai_api_key=OPENAI_API_KEY,\n",
    "    openai_model=OPENAI_MODEL,\n",
    "    openai_token_limit=OPENAI_TOKEN_LIMIT,\n",
    ")\n",
    "documents =  doctrans.parse(content=content)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "72b10af54a08b6c6"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 总结文档\n",
    "summary = documents.summarize(token_limit=100).execute()\n",
    "print(summary.transformed_content)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1a0a211f395faa30"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 翻译文档\n",
    "translation = documents.translate(language=\"chinese\").execute()\n",
    "print(translation.transformed_content)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5e61635020466527"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "## 精炼文档 删除除了某个主题和关键词之外的内容，仅保留与主题相关的内容\n",
    "refinement = documents.refine(topics=['marketing']).execute()\n",
    "print(refinement.transformed_content)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "65091833150a231c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
